﻿/*
 *  Copyright (c) 2012 Brandon Ellenberger
 *
 *  This file is part of OpSatPlan.
 *
 *  OpSatPlan is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpSatPlan is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpSatPlan.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

namespace OpSatPlan
{
    public class AtomicFormulaDefinition
    {
        private ReadOnlyCollection<Parameter> mParameters;
        private PredicateDefinition mPredicate;

        public AtomicFormulaDefinition(PredicateDefinition def, IList<Parameter> parms)
        {
            Assertion.Assert<ArgumentNullException>(def != null, "Predicate Definition cannot be null.");
            this.mPredicate = def;
            Assertion.Assert<ArgumentNullException>(parms != null, "Parameter list cannot be null.");
            this.mParameters = new ReadOnlyCollection<Parameter>(parms);
        }

        public ReadOnlyCollection<Parameter> Parameters
        {
            get
            {
                return this.mParameters;
            }
        }

        public PredicateDefinition Predicate
        {
            get
            {
                return this.mPredicate;
            }
        }

        public override bool Equals(object obj)
        {
            if (obj == null)
                return false;
            else if (obj.GetType() == typeof(AtomicFormulaDefinition))
                return this == (AtomicFormulaDefinition)obj;
            else
                return false;
        }

        public static bool operator ==(AtomicFormulaDefinition x, AtomicFormulaDefinition y)
        {
            if (System.Object.ReferenceEquals(x, y))
                return true;
            else if ((object)x == null || (object)y == null)
                return false;
            else
            {
                bool equal = true;
                if (x.Parameters.Count != y.Parameters.Count)
                    return false;
                else
                {
                    equal = x.Predicate == y.Predicate;
                    for (int i = 0; i < x.Parameters.Count; i++)
                    {
                        equal &= x.Parameters[i] == y.Parameters[i];
                    }
                    return equal;
                }

            }
        }

        public static bool operator !=(AtomicFormulaDefinition x, AtomicFormulaDefinition y)
        {
            return !(x == y);
        }

        public override int GetHashCode()
        {
            int result = 17;
            result = 37 * result + this.mPredicate.GetHashCode();
            foreach (Parameter p in this.Parameters)
            {
                result = 37 * result + p.GetHashCode();
            }
            return result;
        }
    }
}
